package com.legendframework.core.log;

import com.legendframework.core.Store;
import com.legendframework.core.log.enums.LoggerLevelEnum;
import com.legendframework.core.initialization.dao.LogConfigDao;
import com.legendframework.core.initialization.entity.LogConfig;

/**
 * @author 196
 * @Description:
 * @date 2019/12/12 10:01
 */
public class LegendLogger extends AbstractLegendLogger {

    private Store store;

    private LogConfig logConfig;

    public LegendLogger(String serviceCode , Store store) {
        super(serviceCode);
        this.store = store;

    }

    /**
     * 获取log日志文件
     * @return
     */
    private LogConfig getLogConfig(){
        if (logConfig == null) {
            synchronized (this){
                if (logConfig != null) {
                    LogConfigDao logConfigDao = store.getBeansFactory().getBean(LogConfigDao.class);
                    logConfig = logConfigDao.selectOne(null,false);
                }
            }
        }
        return logConfig;
    }

    /**
     * 获取当前系统日志级别
     * @return
     */
    @Override
    LoggerLevelEnum getLoggerLevel() {
        LogConfig log = getLogConfig();
        LoggerLevelEnum loggerLevelEnum = log.getLogLevel();
        if (loggerLevelEnum == null) {
            loggerLevelEnum = LoggerLevelEnum.INFO;
        }
        return loggerLevelEnum;
    }

    /**
     * 获取日志输出模板格式
     * @return
     */
    @Override
    String getTemplateInfo() {
        LogConfig log = getLogConfig();
        return log.getTemplateInfo();
    }
}
